package cn.gov.msa.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;

import cn.gov.msa.model.DeclareItem;
import cn.gov.msa.model.Reserve;
import cn.gov.msa.model.UserDetail;
import cn.gov.msa.service.ReserveService;
import cn.gov.msa.util.GsonHelper;
import cn.gov.msa.util.InfoObjUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "/reserve", produces = "text/plain;charset=UTF-8")
public class ReserveController {
	
	ObjectMapper mapper = new ObjectMapper();
	
	@Autowired
	private ReserveService reserveService;
	
	@RequestMapping(value = "/list")
	public String list(@RequestBody JSONObject params, HttpSession session) throws Exception {	
		
		
		String result = "";		
		int pageNum = 1;
		int pageSize = 10;
		if(params.has("pageNum"))
		{
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if(params.has("pageSize"))
		{
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}
		// 默认查询当前登录账号的所属机构信息
		if("".equals(params.optString("ORGCODE"))){
			UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
			params.put("ORGCODE", userDetail.getOrgcode());
		}
		Reserve reserve = GsonHelper.convertEntity(params.toString(), Reserve.class);
		List<Reserve> reserveList = reserveService.selectData(pageNum, pageSize,reserve);		
	    //获取分页信息
        PageInfo page = new PageInfo(reserveList);       
		JSONArray ja = JSONArray.fromObject(reserveList);				
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);				
		result = jsonResult.toString();
		return result;
	}
	
	
	@RequestMapping(value = "/selectItem")
	public String selectItem(String itemno) throws Exception {	

		String result = "";		
		String info = "";
		JSONObject jsonResult = new JSONObject();
		
		
			List<DeclareItem> list = reserveService.selectItem(itemno);		    
			JSONArray ja = JSONArray.fromObject(list);				
						
			result = list.size() > 0 ? "1" : "0";
			info = list.size() > 0 ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;	
			jsonResult.put("datas", ja);				
						
		
		
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		
		System.err.println(jsonResult);
		
		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/changeState")
	public String changeState(Reserve reserve) throws Exception {	

		JSONObject jsonResult = new JSONObject();
		
		System.out.println( reserve.getRECORDID());
		int result=reserveService.updateState(reserve);
		
		jsonResult.put("result", result);
		
		return jsonResult.toString();
	}
	
}
